package com.puboot.module.admin.media;

import com.puboot.common.util.FileTools;
import com.puboot.common.util.ImgTools;
import com.puboot.module.admin.model.BizFile;
import com.puboot.module.admin.service.BizFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;

/**
 * @Description
 * @Author 申紫微
 * @Data 2020-02-14 14:29:26
 * @Version 1.0
 */
@Controller
@Scope("prototype")
@Slf4j
public class ImgAction extends BaseAction{

    @Autowired
    private BizFileService fileService;

    @Value(value = "${mediaDomain}")
    private String mediaDomain;
    @Value("${ftp.server.path}")
    private String uploadPath;

    @RequestMapping("/images/{fid}")
    public void img(@PathVariable String fid,String w, String h) throws IOException {
        log.debug("fid:{},width:{},height:{}",fid,w,h);
        //设置页面不缓存
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);

        BizFile dbFile = fileService.selectByFileId(fid);
        if(Objects.isNull(dbFile) || dbFile.getStatus() < 0){
            log.info("fid is {} , not fund",fid);
            try {
                response.sendRedirect(mediaDomain.concat("/error/404"));
            } catch (IOException e) {
                log.info(e.getMessage());
            }
            return;
        }
        log.debug("fid:{}",fid);
        FileInputStream fis = null;

        try {
            File img = new File(uploadPath.concat(dbFile.getFileAddr()));
//            File img = new File(dbFile.getFileAddr());
            if(Objects.isNull(img) || !img.exists()){
                response.sendRedirect(mediaDomain.concat("/error/404"));
            }else{
                Integer height = Integer.valueOf(h);
                Integer width = Integer.valueOf(w);
                height = height < 0 ? 0 : height;
                width = width < 0 ? 0 : width;
                if (height == 0 && width == 0) {
                    try {
                        response.sendRedirect(mediaDomain.concat(dbFile.getFileAddr()));
                    } catch (IOException e) {
                        log.info(e.getMessage());
                    }
                }else{
                    ImgTools.thumbnail_w_h(img, width, height, response.getOutputStream());
                }
            }
        } catch (IOException e) {
            log.info(e.getMessage());
        } finally {
            FileTools.close(fis);
        }
    }
}
